<template>
  <div class="login"></div>

  <!--主体内容-->
  <div class="login-mid">
    <div class="login-mid-top">
      <div class="shadow-top-left"></div>
      <div class="shadow-top-right"></div>
    </div>
    <div class="login-mid-mid">
      <!--捕获人脸区域-->
      <div class="videoCamera-canvasCamera">
        <video id="videoCamera" :width="videoWidth" :height="videoHeight" autoplay></video>
        <canvas
          style="display: none"
          id="canvasCamera"
          :width="videoWidth"
          :height="videoHeight"
        ></canvas>

        <!--人脸特效区域-->
        <div v-if="faceImgState" class="face-special-effects-2"></div>
        <div v-else class="face-special-effects"></div>
      </div>

      <!--按钮区域-->
      <div class="face-btn">
        <button @click="faceVef()">{{ faceImgState ? '正在识别中...' : '开始识别' }}</button>
      </div>

      <!--消息区域-->
      <div class="msg">
        <div class="server-msg">{{ msg }}</div>
        <div class="welcome">欢迎来到韶关废品智慧回收平台</div>
      </div>
    </div>
    <div class="login-mid-bot">
      <div class="shadow-bot-left"></div>
      <div class="shadow-bot-right"></div>
    </div>
  </div>
</template>
<script>
import axios from 'axios'
import $camera from '../camera/index'
export default {
  data() {
    return {
      videoWidth: 200,
      videoHeight: 200,
      msg: '',
      faceImgState: false, // 是否开启人脸识别
      faceOption: {}
    }
  },
  mounted() {
    // 调用摄像头
    this.faceOption = $camera.getCamera({
      videoWidth: 200,
      videoHeight: 200,
      thisCancas: null,
      thisContext: null,
      thisVideo: null,
      canvasId: 'canvasCamera',
      videoId: 'videoCamera'
    })
  },
  methods: {
 // 调用后台接口
   faceVef() {
      // 开始绘制图片
      const imageBase = $camera.draw(this.faceOption)
      if (this.faceImgState) {
      return
     }
     this.faceImgState = true
     if (imageBase === '' || imageBase === null || imageBase === undefined) {
      this.$message.error('图片数据为空')
     } else {
      axios.post('/face/vef', { imageBase }, { headers: { 'Content-Type': 'application/json; charset=utf-8' } }).then(res => {
      console.log(res)
      this.faceImgState = false
      // 跳转首页
      if (res.data.code === 200) {
      // 关闭摄像头
     this.faceOption.thisVideo.srcObject.getTracks()[0].stop()
      window.sessionStorage.setItem('face_token', res.data.token)
      window.sessionStorage.setItem('username', res.data.name)
      this.$message.success(res.data.msg)
      this.$router.push('/home')
      }
      if (res.data.code === 201) {
       this.$message.success(res.data.msg)
      }
      }, onerror => {
      this.faceImgState = false
    })
     }
    }
  }
}
</script>
<style lang="less" scoped>
.login {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0px;
  top: 0px;
  background: url(../assets/image/login-bg.png) no-repeat;
  background-size: cover;
  filter: blur(10px);
}

// 登录内容
.login-mid {
  position: absolute;
  width: 45%;
  height: 65%;
  background-color: rgba(37, 76, 102, 0.5);
  transform: translate(-50%, -50%);
  left: 50%;
  top: 50%;
  cursor: pointer;

  .login-mid-top,
  .login-mid-bot {
    width: 100%;
    height: 10%;
  }

  // 2个上边框
  .login-mid-top {
    .shadow-top-left {
      float: left;
      width: 50px;
      height: 100%;
      border-top: 2px solid #00ffff;
      border-left: 2px solid #00ffff;
      filter: blur(2px);
    }
    .shadow-top-right {
      float: right;
      width: 50px;
      height: 100%;
      border-top: 2px solid #00ffff;
      border-right: 2px solid #00ffff;
      filter: blur(2px);
    }
  }

  // 2个下边框
  .login-mid-bot {
    .shadow-bot-left {
      float: left;
      width: 50px;
      height: 100%;
      border-bottom: 2px solid #00ffff;
      border-left: 2px solid #00ffff;
      filter: blur(2px);
    }
    .shadow-bot-right {
      float: right;
      width: 50px;
      height: 100%;
      border-bottom: 2px solid #00ffff;
      border-right: 2px solid #00ffff;
      filter: blur(2px);
    }
  }
}

.login-mid-mid {
  width: 100%;
  height: 80%;

  .videoCamera-canvasCamera {
    position: relative;
    width: 200px;
    height: 200px;
    margin: 0 auto;

    // 人脸识别框
    .face-special-effects,
    .face-special-effects-2 {
      width: 300px;
      height: 300px;
      position: absolute;
      z-index: 99999999999 !important;
      transform: translate(-50%, -50%);
      top: 50%;
      left: 50%;
    }

    // 开启摄像头
    .face-special-effects {
      background: url(../assets/image/face.gif) no-repeat;
      background-size: 300px 300px;
    }
    // 没有开启摄像头
    .face-special-effects-2 {
      background: url(../assets/image/face_detection.gif) no-repeat;
      background-size: 300px 300px;
    }
  }
}

// 经过识别框边框变白
.login-mid:hover .shadow-top-left,
.login-mid:hover .shadow-top-right,
.login-mid:hover .shadow-bot-left,
.login-mid:hover .shadow-bot-right {
  border-color: #fff;
  filter: blur(1px);
}

.face-btn {
  width: 100%;
  height: 50px;
  z-index: 999999999999 !important;
  margin-top: 45px;
  text-align: center;

  button {
    background-color: #00ffff;
    border: none;
    outline: none;
    width: 200px;
    height: 40px;
    border-radius: 6px;
    color: #ffffff;
    cursor: pointer;
  }
}

.msg {
  width: 100%;
  height: 75px;
  color: #ffffff;

  .server-msg {
    width: 100%;
    height: 35px;
    text-align: center;
    line-height: 35px;
  }

  .welcome {
    width: 100%;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 20px;
    color: white;
    font-family: serif;
  }
}
</style>
